package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.YanShouDaiBean;
import com.wnhz.work.yz.bean.YanShouDaiDetailsBean;
import com.wnhz.work.yz.utils.GlideImageLoader;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class YanShouGaiActivity extends BaseActivity implements View.OnClickListener {
    private RecyclerView recycler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yan_shou_gai);
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("整改");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        recycler = (RecyclerView) findViewById(R.id.recycler);
        recycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        upLoad();

    }

    private List<YanShouDaiBean.InfoEntity> info1 = new ArrayList<>();

    private void upLoad() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("keywords", "");
        showDialog();

        XUtil.Post(Url.DECORATE_CHECKNOTOKLIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    Log.e("----", "onSuccess:= " + info);
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        YanShouDaiBean bean = gson.fromJson(result, YanShouDaiBean.class);
                        info1 = bean.getInfo();
                        setData();
                        recycler.setVisibility(View.VISIBLE);
                    } else {
                        recycler.setVisibility(View.GONE);
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();
            }
        });


    }

    private void setData() {
        recycler.setAdapter(
                new BaseRVAdapter(this, info1) {
                    @Override
                    public int getLayoutId(int viewType) {
                        return R.layout.item_yanshou_dai;
                    }

                    @Override
                    public void onBind(BaseViewHolder holder, final int position) {

                        if (info1.get(position).getImg() != null && info1.get(position).getImg().size() > 0) {
                            //   x.image().bind(img, info.getImg().get(0));
                            Banner img = holder.getView(R.id.img2);
                            img.setImageLoader(new GlideImageLoader());
                            //设置图片集合
                            img.setImages(info1.get(position).getImg());
                            img.setBannerStyle(BannerConfig.NOT_INDICATOR);
                            //banner设置方法全部调用完毕时最后调用
                            img.start();
                        }

                        holder.getTextView(R.id.name).setText(info1.get(position).getProject_name());
                        holder.getTextView(R.id.hetonghao).setText("" + info1.get(position).getBudget_no());
                        holder.getTextView(R.id.time).setText("" + info1.get(position).getCheck_time());
                        holder.getTextView(R.id.address).setText("" + info1.get(position).getAddress());
                        holder.getTextView(R.id.anzhuang).setText("安装队：" + info1.get(position).getInstall());
                        holder.getTextView(R.id.or).setText("总套数：" + info1.get(position).getAll_nums() + "套");

                        holder.getView(R.id.ll_max).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                getDetails(position);
                            }
                        });

                    }

                }


        );
    }

    private void getDetails(final int position) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("orderid", info1.get(position).getOrderid());
        showDialog();

        XUtil.Post(Url.DECORATE_WAITINGCHECKDETAIL, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    Log.e("----", "onSuccess:= " + info);
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        YanShouDaiDetailsBean bean = gson.fromJson(result, YanShouDaiDetailsBean.class);
                        startActivity(new Intent(YanShouGaiActivity.this, YanShouDaiChanPinActivity.class).putExtra("bean", bean));
                        //添加关联数据库2，子类数据库
                        //Todo 添加关联数据库2
                        //addDb2(position, bean);


                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();
            }
        });


    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
        }
    }
}
